﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace HHHFramework {
    public class ResData :Singleton<ResData>{
        private ResData() {
            Load();
        }
        public void Ini()
        {
            Debug.Log("ResData Ini");
        }
        public List<AssetBundleData> AssetbundleDatas = new List<AssetBundleData>();

        private static AssetBundleManifest mManifest;

        
        void Load() {

            if (ResMgr.IsSimulationModelogic)
            {
#if UNITY_EDITOR
                AssetbundleDatas.Clear();
                var assetbundleNames = UnityEditor.AssetDatabase.GetAllAssetBundleNames();
                foreach (var assetBundleName in assetbundleNames)
                {

                    var assetbundlePaths = UnityEditor.AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleName);
                    var assetBundleData = new AssetBundleData()
                    {
                        Name = assetBundleName,
                        DependencyBundleNames = UnityEditor.AssetDatabase.GetAssetBundleDependencies(assetBundleName, false)
                    };
                    foreach (var assetPath in assetbundlePaths)
                    {
                        var assetData = new AssetData()
                        {
                            Name = assetPath.Split('/').Last().Split('.').First(),//UnityEditor.AssetDatabase.LoadAssetAtPath<Object>(assetPath).name,
                            OwnerBuundleName = assetBundleName
                        };
                        assetBundleData.assetDataList.Add(assetData);
                    }
                    AssetbundleDatas.Add(assetBundleData);
                }

                AssetbundleDatas.ForEach(assetbundleData =>
                {
                    assetbundleData.assetDataList.ForEach(assetData =>
                    {
                        Debug.Log($"AB:{assetbundleData.Name}   assetDataName:{assetData.Name}  onwerBundle:{assetData.OwnerBuundleName}");
                    });
                    foreach (var dependencyBundleName in assetbundleData.DependencyBundleNames)
                    {
                        Debug.Log($"AB:{assetbundleData.Name}   dependencyBundleName:{dependencyBundleName}");
                    }
                });
#endif
            }
            else
            {
                ///获取main assetbundle
                // string path = ResKitUtil.FullPathForAssetBundle(ResKitUtil.GetPlatformName());
                string path = ResKitUtil.FullPathForAssetBundle("StandaloneWindows");
              
                Debug.Log(path);
                var mainBundle = AssetBundle.LoadFromFile(path);
                mManifest = mainBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
                for (int i = 0; i < mManifest.GetAllAssetBundles().Count(); i++)
                {
                    Debug.Log("assetbundle:" + mManifest.GetAllAssetBundles()[i]);
                }

            }
        }

        public string[] GetDirectDependencies(string bundleName) {
            if (ResMgr.IsSimulationModelogic)
            {
                return AssetbundleDatas.Find(abdata => abdata.Name == bundleName).DependencyBundleNames;
            }
            else {
                return mManifest.GetDirectDependencies(bundleName);
            }
           
        }

    }
}

